import { getUserChannels, getAllChannels } from '@/api/channel'
export default {
  namespaced: true,
  state: {
    channelList: [],
    allChannelList: []
  },
  mutations: {
    setChannelList(state, payload) {
      state.channelList = payload
    },
    setAllChannelList(state, payload) {
      state.allChannelList = payload
    },
    addChannelList(state, payload) {
      state.channelList.push(payload)
    },
    delChannelList(state, payload) {
      // !删除我的频道过滤出每一项的id与传参过来的id不等的
      state.channelList = state.channelList.filter(
        (item) => item.id !== payload.id
      )
    }
  },
  actions: {
    async getChannelListAsync(context) {
      const res = await getUserChannels()
      // console.log(res)
      context.commit('setChannelList', res.data.channels)
    },
    async getAllChannelListAsync(context) {
      const res = await getAllChannels()
      // console.log(res)
      context.commit('setAllChannelList', res.data.channels)
    }
  },
  getters: {
    optionsChannelList(state) {
      return state.allChannelList.filter((item) => {
        const res = state.channelList.some((channel) => channel.id === item.id)
        if (res) {
          return false
        } else {
          return true
        }
      })
    }
  }
}
